package com.onetech.adrewards.free.adapters;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fedorvlasov.lazylist.ImageLoader;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.objects.AdDTO2;
import com.onetech.adrewards.free.utils.AdrewadsUtils;


public class ListAdRewardAdapter2 extends BaseAdapter {
	private class AppViewHolder {
		public TextView title;
		public TextView charge_type;		
		public TextView max_point;
		public TextView charge_cate;
		public TextView rec_cate;
		public TextView camp_cate;
		public TextView rec_text;
		public TextView txtAdsText;
		public ImageView cover;
		public ImageView icon_mark;
	}

	private Activity activity;
	private List<AdDTO2> listApps;
	private static LayoutInflater inflater = null;

	public ListAdRewardAdapter2(Activity _activity) {
		activity = _activity;
		listApps = new ArrayList<AdDTO2>();
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void updateAppList(List<AdDTO2> _listAppDTOs) {
		listApps.clear();
		listApps.addAll(_listAppDTOs);
		notifyDataSetChanged();
	}

	public void insertAppList(List<AdDTO2> _listAppDTOs) {
		listApps.addAll(_listAppDTOs);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listApps.size();
	}

	@Override
	public AdDTO2 getItem(int arg0) {
		if (listApps == null) {
			return null;
		}
		return listApps.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AppViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_row_list_adreward,
					parent, false);
			viewHolder = new AppViewHolder();
			viewHolder.cover = (ImageView) convertView
					.findViewById(R.id.appCover);
			viewHolder.icon_mark = (ImageView) convertView
					.findViewById(R.id.iconMark);
			viewHolder.title = (TextView) convertView
					.findViewById(R.id.appName);	
			viewHolder.charge_type = (TextView) convertView
					.findViewById(R.id.charge_type);
			viewHolder.max_point = (TextView) convertView
					.findViewById(R.id.appPoints);
			viewHolder.charge_cate = (TextView) convertView
					.findViewById(R.id.chargeCate);
			viewHolder.rec_cate = (TextView) convertView
					.findViewById(R.id.recCate);
			viewHolder.camp_cate = (TextView) convertView
					.findViewById(R.id.campCate);
			viewHolder.rec_text = (TextView) convertView
					.findViewById(R.id.recText);
			viewHolder.txtAdsText = (TextView) convertView
					.findViewById(R.id.txtAdsBtn);
//			viewHolder.date_created = (TextView) convertView
//					.findViewById(R.id.dateCreated);	
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (AppViewHolder) convertView.getTag();
		}
		
		AdDTO2 temp = listApps.get(position);
		viewHolder.title.setText(temp.name);
		
		if(temp.is_app!=null && temp.is_app.equals("1")){
			viewHolder.txtAdsText.setText(activity.getString(R.string.button_ads_install_label));
			viewHolder.txtAdsText.setBackgroundResource(R.drawable.button_blue4);
		}else{
			viewHolder.txtAdsText.setText(activity.getString(R.string.button_ads_get_label));
			viewHolder.txtAdsText.setBackgroundResource(R.drawable.button_orange2);
		}
		
		if(temp.charge_cate==null || temp.charge_cate.trim().length()<1 || temp.charge_cate.equals("null")){
			viewHolder.charge_cate.setVisibility(View.GONE);
		}else{
			viewHolder.charge_cate.setText(temp.charge_cate);
			viewHolder.charge_cate.setVisibility(View.VISIBLE);
		}
		if(temp.rec_cate==null || temp.rec_cate.trim().length()<1 || temp.rec_cate.equals("null")){
			viewHolder.rec_cate.setVisibility(View.GONE);
		}else{
			viewHolder.rec_cate.setText(temp.rec_cate);
			viewHolder.rec_cate.setVisibility(View.VISIBLE);
		}
		if(temp.camp_cate==null || temp.camp_cate.trim().length()<1 || temp.camp_cate.equals("null")){
			viewHolder.camp_cate.setVisibility(View.GONE);
		}else{
			viewHolder.camp_cate.setText(temp.camp_cate);
			viewHolder.camp_cate.setVisibility(View.VISIBLE);
		}
		if(temp.rec_text==null || temp.rec_text.trim().length()<1|| temp.rec_text.equals("null")){
			viewHolder.rec_text.setVisibility(View.GONE);
		}else{
			viewHolder.rec_text.setText(temp.rec_text);
			viewHolder.rec_text.setVisibility(View.VISIBLE);
		}
		String mIconMark=temp.icon_mark;
		
		if(mIconMark!=null && mIconMark.trim().length()>0){
			if(mIconMark.trim().equals("1")){
				viewHolder.icon_mark.setBackgroundResource(R.drawable.icon_mark_new);
				viewHolder.icon_mark.setVisibility(View.VISIBLE);
			}else{
				if(mIconMark.trim().equals("2")){
					viewHolder.icon_mark.setBackgroundResource(R.drawable.icon_mark_recommend);
					viewHolder.icon_mark.setVisibility(View.VISIBLE);
				}else{
					viewHolder.icon_mark.setVisibility(View.GONE);
				}
			}			
		}else{
			viewHolder.icon_mark.setVisibility(View.GONE);
		}
//		String charge_type=AdrewadsUtils.getChargeTypeText(activity,temp.charge_type);
//		String short_desc="";
//		if(temp.short_desc.trim().length()<1){
//			
//		}else{
//			short_desc+=" | " +temp.short_desc;
//		}
//		
//		viewHolder.charge_type.setText(charge_type + short_desc);
		
		viewHolder.charge_type.setText(Html.fromHtml(temp.short_desc));
		
		int sale_coin=AdrewadsUtils.convertString2Int(temp.sale_coin);
		String textCoin="";		
		if(sale_coin>0){
			if(temp.is_max.trim().equals("1")){
				//textCoin=activity.getString(R.string.max_coin_text)+temp.coin+"pt"+"→"+temp.sale_coin + "pt";
				textCoin=activity.getString(R.string.max_coin_text)+temp.sale_coin + "pt";
			}else{
				//textCoin=temp.coin+"pt"+"→"+temp.sale_coin + "pt";
				textCoin=temp.sale_coin + "pt";
			}
		}else{
			if(temp.is_max.trim().equals("1")){
				textCoin=activity.getString(R.string.max_coin_text)+temp.coin+"pt";
			}else{
				textCoin=temp.coin+"pt";
			}
		}
		viewHolder.max_point.setText(Html.fromHtml(textCoin));
		//viewHolder.date_created.setText(temp.getDis_expired_end_date());
		ImageLoader.getInstance(activity.getApplicationContext()).DisplayImage(
				temp.img, viewHolder.cover);

		return convertView;
	}

}

